import re

# 全局变量，用于标记是否遇到特定行
g_get_table = 0

g_video_index = 1

bilibili_id = "BV1HT421k7GW"
git_name = "01_all_series_quickstart"

def process_line(line):
    global g_get_table
    global g_video_index
    global bilibili_id
    global git_name
    
    # 判断是否为特定行
    if line.strip() == "| ---- | ---- | ---- | ---- |":
        g_get_table = 1
        return line
    
    if g_get_table == 0:
        return line

    # 使用正则表达式匹配小括号内的内容
    pattern = r'\((.*?)\)'
    matches = re.findall(pattern, line)
    
    for i, match in enumerate(matches):
        if i == 0:
            # 第一个小括号内的内容替换为指定视频链接
            new_match = "https://www.bilibili.com/video/"+ bilibili_id + "/?p=" + str(g_video_index)
            g_video_index += 1
        else:
            tmp_match = match.replace('..\\第1~6篇的GIT仓库\\01_all_series_quickstart\\', '')
            # 将反斜杠替换为正斜杠
            new_match = tmp_match.replace('\\', '/')
            # 判断是否包含 .pdf
            if '.pdf' not in new_match:
                # 在前面加上Git地址
                new_match = f"https://gitee.com/weidongshan/"+git_name+f"/tree/master/{new_match}"
            else:
                new_match = f"https://gitee.com/weidongshan/"+git_name+f"/raw/master/{new_match}"
        # 替换原行中的内容
        line = line.replace(f"({match})", f"({new_match})", 1)
    
    return line

def main():
    input_file = "视频_文档_源码对照表.md"
    output_file = "视频_文档_源码对照表_online.md"
    
    with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
        for line in infile:
            processed_line = process_line(line)
            outfile.write(processed_line)

if __name__ == "__main__":
    main()